<?php

namespace app\cli\controller;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use addons\goodhelper\model\VslGoodsHelpModel;
use addons\goodhelper\server\GoodHelper;

class GoodsTaskCalculate_3 extends Command {

    protected function configure() {
        $this->setName('goods_task_calculate_3')->setDescription('商品队列导入3');
    }

    /**
     * 启动服务端服务
     * @return \lib\crontab\IssServer
     */
    public function execute(Input $input, Output $output) {
        //商品导入代码
        while (1) {
            try {
                $goodsHelp = new VslGoodsHelpModel();
                $firstData = $goodsHelp->getQuery(['status' => ['<>', 2]], '*', 'create_time asc');
                if (!$firstData) {
                    sleep(2);
                    continue;
                }
                foreach ($firstData as $val) {
                    if (!$val || $val['status'] == 1) {//没有数据或者有数据在执行就则结束
                        continue;
                    }
                    $goodsHelp->save(['status' => 1], ['help_id' => $val['help_id']]);
                    $goodsHelpServer = new GoodHelper();
                    $res = $goodsHelpServer->addGoodsByXls($val['file_name'], $val['zip_name'], $val['add_type'], $val['type'], $val['shop_id'], $val['website_id']);
                    if ($res['code'] != 1) {
                        $goodsHelp->save(['error_info' => $res['message'], 'status' => 2], ['help_id' => $val['help_id']]);
                        continue;
                    }
                    $goodsHelp->delData(['help_id' => $val['help_id']]);
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $log_dir = getcwd() . '/goods_import.log';
                file_put_contents($log_dir, date('Y-m-d H:i:s') . $msg . PHP_EOL, 8);
            }
        }
    }

}

?>